<template>
  <div class="login-container">
    <canvas id="canvas" class="snow"></canvas>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">城市车流动态调控与信号管理</h3>
      </div>

      <el-form-item prop="account" v-if="isLogin">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="请输入account账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码至少6位"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="isLogin && handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <template v-if="!isLogin">
        <el-form-item prop="repassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.repassword"
            :type="passwordType"
            placeholder="请确认密码"
            name="repassword"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>   
        <el-form-item prop="userName" >
          <el-input
            v-model="loginForm.userName"
            placeholder="请输入昵称"
            tabindex="2"
          />
        </el-form-item> 
        <el-form-item prop="phoneNumber" >
          <el-input
          type="tel"
            v-model="loginForm.phoneNumber"
            placeholder="请输入手机号"
            tabindex="2"
          />
        </el-form-item>  
        <el-form-item prop="email" >
          <el-input
          type="email"
            v-model="loginForm.email"
            placeholder="请输入邮箱"
            tabindex="2"
          />
        </el-form-item>  
      </template>
      <el-form-item>
        <el-radio-group v-model="loginForm.userType">
          <el-radio label="NORMAL_USER">普通用户</el-radio>
          <el-radio label="MANAGER_USER">交通管理人员</el-radio>
          <el-radio label="PLAN_USER">市政规划人员</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="register" @click="bindLoginorRegister">
        {{ isLogin ? '去注册' : '去登录' }}
      </div>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ isLogin ? '登录' : '注册' }}</el-button>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { active } from '@/utils/active'
import { register } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      isLogin: true, // 是否登录界面 true为登录 false为注册
      loginForm: {
        account: '',
        password: '',
        repassword: '',
        userName: '',
        phoneNumber: '',
        email: '',
        userType: 'NORMAL_USER'
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', validator: this.validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: this.validatePassword }],
        repassword: [{ required: true, trigger: 'blur', validator: this.validaterePassword }],
        userName: [{ required: true, trigger: 'blur', message: '请输入昵称' }],
        phoneNumber: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    
  },
  mounted(){
    active()
  },
  methods: {
    bindLoginorRegister(){
      this.isLogin = !this.isLogin
      if(!this.isLogin){
        this.$nextTick(() => {
          this.$refs.account.focus()
        })
      }else{

      }
      this.resetForm();
    },
    validateUsername(rule, value, callback){
      if (!validUsername(value)) {
        callback(new Error('账号至少3个字符，且只能包含字母、数字、下划线'))
      } else {
        callback()
      }
    },
    validatePassword(rule, value, callback){
      if (value.length < 6) {
        callback(new Error('密码至少6位'))
      } else {
        callback()
      }
    },
    validaterePassword(rule, value, callback){
      if (this.loginForm.password !== value) {
        callback(new Error('两次密码输入不一致！'))
      } else {
        callback()
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      const isLogin = this.isLogin;
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          // 注册
          if(!isLogin){
            const { code = undefined, result = undefined } = await register({ ...this.loginForm })
            this.loading = false
            if(code === 200){
              this.$message.success('注册成功，请登录')
              this.isLogin = true
              this.resetForm();
              this.loginForm.account = result.account
              this.$refs.account.focus()
            }
            return
          }
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.loginForm.resetFields()
      this.loginForm = {
        account: '',
        password: '',
        repassword: '',
        userName: '',
        phoneNumber: '',
        email: '',
        userType: 'NORMAL_USER'
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}


.register{
  margin-bottom: 30px;
  color: #fff;
  text-align: end;
  cursor: pointer;
  font-size: 14px ;
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px rgba(30,30,59) inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 100px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.snow {
	position: absolute;
	top: 0;
	left: 0;
  background: linear-gradient(transparent,rgba(#000, 0.5)),
    linear-gradient(to right,#224,#336,#224);
}
</style>
